package com.digitalchina.lxnsy.activity.old;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessPayPassword;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.constant.TIME;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.widget.EditTextWithClear;
import com.digitalchina.lxnsy.xml.UserXML;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/9/30 11:11
 * 修改备注：
 * ================================================
 */
public class SetPayPasswordOneActivity extends BaseActivity {
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.et_code)
    EditTextWithClear etCode;
    @Bind(R.id.bt_code)
    Button btCode;
    @Bind(R.id.bt_next)
    Button btNext;

    private String mPhone = "";
    private int type;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_set_pay_password);
        setTitle("设置支付密码");
    }

    @Override
    public void initMember() {
        // 0 为首次设置支付密码  1为忘记密码
        type = (int) getIntent().getSerializableExtra("type");


        mPhone = UserXML.getMobilePhone(this);
        tvPhone.setText(mPhone);
    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_PAY_PASSWORD_SEND_CODE_SUCCESS:  // 发送验证码
                        Utils.startForbitSendVerifyCode(SetPayPasswordOneActivity.this,
                                mHandler, TIME.SEND_CHECKCODE_TIME, null);
                        break;
                    case MSG.MSG_PAY_PASSWORD_SEND_CODE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_SEND_VERIFY_CODE_TIP: // 验证码倒计时中
                        if (isFinishing())
                            return;
                        btCode.setClickable(false);
                        btCode.setTextColor(Color.parseColor("#b3b3b3"));
                        btCode.setText(String.valueOf(msg.obj));
                        btCode.setBackgroundResource(R.drawable.shape_bt_auth_code_disable);
                        break;
                    case MSG.MSG_SEND_VERIFY_CODE_END: // 验证码倒计时结束
                        if (isFinishing())
                            return;
                        btCode.setClickable(true);
                        btCode.setTextColor(getResources().getColor(R.color.app_color));
                        btCode.setText("发送验证码");
                        btCode.setBackgroundResource(R.drawable.shape_rect_app_color);
                        break;
                    case MSG.MSG_PAY_PASSWORD_CHECK_CODE_SUCCESS: // 验证填写的验证码
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_PAY_PASSWORD_CHECK_CODE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        GotoUtil.gotoActivity(this, SetPayPasswordTwoActivity.class, true, "type", type);
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.bt_code, R.id.bt_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_code:
                getAuthCode();
                break;
            case R.id.bt_next:
                next();
                break;
        }
    }


    /**
     * 获取输入内容 点击下一步
     */
    private void next() {

        String mStrAuthCode = StringUtil.getEditText(etCode);


        if (StringUtil.isStrEmpty(mStrAuthCode)) {
            showToast("请填写验证码");
            return;
        }

        BusinessPayPassword.sendVercheckVerifyCodeifyCode(this, mPhone, mStrAuthCode, mHandler);
    }

    /**
     * 获取验证码
     */
    private void getAuthCode() {


        BusinessPayPassword.sendVerifyCode(this, mPhone, mHandler);

    }
}
